package com.yg.controller;


import com.yg.config.result.R;
import com.yg.entity.readAndWriteTemplates.CourseBuildRead;
import com.yg.entity.singleTemplates.CourseBuild;
import com.yg.service.CourseBuildService;
import com.yg.utils.CloseStream;
import com.yg.utils.DownLoadUtil;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * <p>
 * 对应sheet09，此表为课程建设表 前端控制器
 * </p>
 *
 * @author YingGu
 * @since 2020-11-04
 */
@RestController
@RequestMapping("/courseBuild")
@CrossOrigin
public class CourseBuildController {

    @Autowired
    private CourseBuildService courseBuildService;

    /**
     * 读取excel写入数据库
     *
     * @param file
     * @return
     */
    @PostMapping("/readExcel")
    public R readExcel(MultipartFile file) {
        //获取fileInputStream
        FileInputStream fileInputStream = null;
        R r = null;
        try {
            fileInputStream = (FileInputStream) file.getInputStream();
            r = courseBuildService.readExcel(fileInputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            CloseStream.closeFileInputStream(fileInputStream);
        }
        return r;
    }

    /**
     * 导出excel
     *
     * @param response
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping("/writeExcel")
    public R writeExcel(HttpServletResponse response, @RequestParam(required = false) String name,String head,String players,String category,String level) throws UnsupportedEncodingException {
        CourseBuild courseBuild = new CourseBuild();
        courseBuild.setName(name);
        courseBuild.setHead(head);
        courseBuild.setPlayers(players);
        courseBuild.setCategory(category);
        courseBuild.setLevel(level);
        R r = courseBuildService.writeExcel(response,courseBuild);
//        r.message(DownLoadUtil.downLoadFile(response));
        return r;
    }

    /**
     * 条件查询
     * 查询所有
     *
     * @param courseBuildRead
     * @return
     */
    @PostMapping("/selectCourseBuildByCondition")
    public R courseBuildConditionalQuery(String current, String limit, @RequestBody(required = false) CourseBuildRead courseBuildRead) {
        R r = null;
        if (courseBuildRead == null) {
            r = courseBuildService.selectAllCourseBuild(current, limit);
        } else {
            r = courseBuildService.courseBuildConditionalQuery(courseBuildRead, current, limit);
        }
        return r;
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/getCourseBuild")
    public R selectAllCourseBuild(String current, String limit) {
        R r = courseBuildService.selectAllCourseBuild(current, limit);
        return r;
    }

    /**
     * 根据id删除教师信息
     *
     * @param
     * @return
     */
    @DeleteMapping("/deleteCourseBuild")
    public R deleteCourseBuildById(@RequestBody CourseBuild courseBuild) {
        System.out.println("---------------------" + courseBuild);
        R r = courseBuildService.deleteCourseBuildById(courseBuild);
        return r;
    }

    /**
     * 根据id更新课程信息
     *
     * @param
     * @return
     */
    @PutMapping("/putCourseBuild")
    public R updateCourseBuildById(@RequestBody CourseBuild courseBuild) {
        R r = courseBuildService.updateCourseBuild(courseBuild);
        return r;
    }

    /**
     * 添加
     *
     * @param
     * @return
     */
    @PostMapping("/postCourseBuild")
    public R insertCourseBuild(@RequestBody CourseBuild courseBuild) {
        R r = courseBuildService.insertCourseBuild(courseBuild);
        return r;
    }
}

